home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Add-Ons / After Dark / Twist 1.0b1 / ADTestbed.c < prev    next >
C/C++ Source or Header  |  1996-06-29  |  11KB  |  565 lines

  1. /* ADTestbed.c
  2.  * Test bed to allow source code debugging of After Dark modules.
  3.  * Copyright ©1996 Michael D. Crawford.  All Rights Reserved.
  4.  * You may use this code on the condition that you read the following web page:
  5.  * http://www.scruznet.com/~crawford/WordServices/wdsvindex.html
  6.  */
  7.  
  8. #include "GraphicsModule_Types.h"
  9. #include "ADTestbed.h"
  10.  
  11. typedef struct {
  12.     GMParamBlockPtr    paramPtr;
  13.     Handle            storage;
  14.     Boolean            running;
  15.     RgnHandle        blankRgn;
  16. } tTBRec, *tTBPtr;
  17.  
  18. void InitManagers( void );
  19. void EventLoop( void );
  20. void DoMouseDown( EventRecord *evtPtr );
  21. void DoKeyDown( EventRecord *evtPtr );
  22. void PutUpMenus( void );
  23. void DoMenuCommand( long theLong );
  24. void DoAppleMenu( short theItem );
  25. void DoFileMenu( short theItem );
  26. void DoEditMenu( short theItem );
  27. void DoTestbedMenu( short theItem );
  28. void DoNewWindow( void );
  29. void RequestQuit( void );
  30. void DoUpdate( WindowPtr win );
  31. void DoCloseWindow( void );
  32. tTBPtr NewTBRec( GrafPtr port, Rect *globalBoundsPtr, Rect *localBoundsPtr );
  33. void SendMessage( WindowPtr docWindow, short message );
  34. void DoIdleEvent( void );
  35. void FixMenuMarks( void );
  36.  
  37. #define kMBarID    128
  38. enum{
  39.     kAppleMenuID = 128,
  40.     kFileMenuID,
  41.     kEditMenuID,
  42.     kTestbedMenuID
  43. };
  44.  
  45. enum{
  46.     kAMAbout = 1,
  47.     kAMDash1
  48. };
  49.  
  50. enum{
  51.     kFMNew = 1,
  52.     kFMClose,
  53.     kFMDash1,
  54.     kFMQuit
  55. };
  56.  
  57. enum{
  58.     kEMUndo = 1,
  59.     kEMDash1,
  60.     kEMCut,
  61.     kEMCopy,
  62.     kEMPaste
  63. };
  64.  
  65. enum{
  66.     kTMInitialize = 1,
  67.     kTMBlank,
  68.     kTMDrawframe,
  69.     kTMClose,
  70.     kTMButtonMessage,
  71.     kTMModuleSelected,
  72.     kTMDoHelp,
  73.     kTMDash1,
  74.     kTMRun
  75. };
  76.  
  77. static Boolean    gQuitRequested;
  78. static WindowPtr    gDocWindow;
  79.  
  80. void main( void )
  81. {
  82.     InitManagers();
  83.     
  84.     gQuitRequested = false;
  85.  
  86.     PutUpMenus();
  87.  
  88.     EventLoop();
  89.     
  90.     ExitToShell();
  91. }
  92.  
  93. void EventLoop( void )
  94. {
  95.     EventRecord    evt;
  96.     
  97.     do{
  98.     
  99.         WaitNextEvent( everyEvent, &evt, 1, (RgnHandle)NULL );
  100.  
  101.         switch ( evt.what ){
  102.             case nullEvent:
  103.                 DoIdleEvent();
  104.                 break;
  105.             case mouseDown:
  106.                 DoMouseDown( &evt );
  107.                 break;
  108.             case mouseUp:
  109.                 break;
  110.             case keyDown:
  111.                 DoKeyDown( &evt );
  112.                 break;
  113.             case keyUp:
  114.                 break;
  115.             case autoKey:
  116.                 break;
  117.             case updateEvt:
  118.                 DoUpdate( (WindowPtr)evt.message );
  119.                 break;
  120.             case diskEvt:
  121.                 break;
  122.             case activateEvt:
  123.                 break;
  124.             case osEvt:
  125.                 break;
  126.         }
  127.         
  128.     }while ( !gQuitRequested );
  129.     
  130.     return;
  131. }
  132.  
  133. void DoAppleMenu( short theItem )
  134. {
  135.     return;
  136. }
  137.  
  138. void DoFileMenu( short theItem )
  139. {
  140.     switch( theItem ){
  141.         case kFMNew:
  142.             DoNewWindow();
  143.             break;
  144.         case kFMClose:
  145.             DoCloseWindow();
  146.             break;
  147.         case kFMDash1:
  148.             break;
  149.         case kFMQuit:
  150.             RequestQuit();
  151.             break;
  152.     }
  153.  
  154.     return;
  155. }
  156.  
  157. void DoEditMenu( short theItem )
  158. {
  159.     return;
  160. }
  161.  
  162. void DoTestbedMenu( short theItem )
  163. {
  164.     switch( theItem ){
  165.         case kTMInitialize:
  166.             SendMessage( gDocWindow, Initialize );
  167.             break;
  168.         case kTMBlank:
  169.             SendMessage( gDocWindow, Blank );
  170.             break;
  171.         case kTMDrawframe:
  172.             SendMessage( gDocWindow, DrawFrame );
  173.             break;
  174.         case kTMClose:
  175.             SendMessage( gDocWindow, Close );
  176.             break;
  177.         case kTMButtonMessage:
  178.             // STUB get this from a dialog or modeless control panel dialog
  179.             SendMessage( gDocWindow, ButtonMessage );
  180.             break;
  181.         case kTMModuleSelected:
  182.             break;
  183.         case kTMDoHelp:
  184.             SendMessage( gDocWindow, DoAbout );
  185.             break;
  186.         case kTMDash1:
  187.             break;
  188.         case kTMRun:
  189.             if ( ( (tTBPtr)( (WindowPeek)gDocWindow )->refCon )->running ){
  190.                 ( (tTBPtr)( (WindowPeek)gDocWindow )->refCon )->running = false;
  191.             }else{
  192.                 ( (tTBPtr)( (WindowPeek)gDocWindow )->refCon )->running = true;
  193.             }
  194.             break;
  195.     }
  196.  
  197.     return;
  198. }
  199.  
  200. void DoIdleEvent( void )
  201. {
  202.     if ( gDocWindow ){
  203.         if ( ( (tTBPtr)( (WindowPeek)gDocWindow )->refCon )->running ){
  204.             SendMessage( gDocWindow, DrawFrame );        
  205.         }
  206.     }
  207.     
  208.     return;
  209. }
  210.  
  211. void SendMessage( WindowPtr docWindow, short message )
  212. {
  213.     tTBPtr    tbPtr;
  214.     OSErr    err;
  215.     
  216.     tbPtr = (tTBPtr)( (WindowPeek)docWindow )->refCon;
  217.  
  218.     err = ADEntry( &tbPtr->storage, tbPtr->blankRgn, message, tbPtr->paramPtr );
  219.     if ( err ){
  220.         DebugStr( "\pGot an error from After Dark module" );
  221.     }
  222.     
  223.     return;
  224. }
  225.  
  226. void DoMenuCommand( long menuSpot )
  227. {
  228.     short theMenu;
  229.     short theItem;
  230.     Boolean result;
  231.         
  232.     theMenu = HiWord( menuSpot );
  233.     
  234.     theItem = LoWord( menuSpot );
  235.     
  236.     switch ( theMenu ){
  237.         case 0:
  238.             break;
  239.         case kAppleMenuID:
  240.             DoAppleMenu( theItem );
  241.             break;
  242.         case kFileMenuID:
  243.             DoFileMenu( theItem );
  244.             break;
  245.         case kEditMenuID:
  246.             DoEditMenu( theItem );
  247.             break;
  248.         case kTestbedMenuID:
  249.             DoTestbedMenu( theItem );
  250.             break;
  251.     }
  252.     
  253.     HiliteMenu( 0 );
  254.     return;
  255. }
  256.  
  257. void DoMouseDown( EventRecord *eventPtr )
  258. {
  259.     WindowPtr        theWindow;
  260.     Rect            dragRect;
  261.     short            deskPart;
  262.     
  263.     deskPart = FindWindow( eventPtr->where, &theWindow );
  264.     switch( deskPart ){
  265.         case inDesk:
  266.             break;
  267.         case inMenuBar:
  268.             FixMenuMarks();
  269.             DoMenuCommand( MenuSelect( eventPtr->where ) );
  270.             break;
  271.         case inSysWindow:
  272.             SystemClick( eventPtr, theWindow );
  273.             break;
  274.         case inContent:
  275.             if ( theWindow != FrontWindow() ){
  276.                 SelectWindow( theWindow );
  277.             } else {
  278.             }
  279.             break;
  280.         case inDrag:
  281.             DragWindow( theWindow, eventPtr->where, &qd.screenBits.bounds );    /* NOT correct but works */
  282.             break;
  283.         case inGrow:
  284.             //GrowDocWindow( theWindow, eventPtr->where );
  285.             break;
  286.         case inGoAway:
  287.             if ( TrackGoAway( theWindow, eventPtr->where ))
  288.                 DoCloseWindow();
  289.             break;
  290.         case inZoomIn:
  291.         case inZoomOut:
  292.             //if ( TrackBox( theWindow, eventPtr->where, deskPart ) )
  293.             //    DoZoom( theWindow, deskPart );
  294.             break;
  295.     }
  296.     return;
  297. }
  298.  
  299. void DoKeyDown( EventRecord *evtPtr )
  300. {
  301.     char        theChar;
  302.     
  303.     theChar = evtPtr->message & charCodeMask;
  304.     
  305.     if ( evtPtr->modifiers & cmdKey ){
  306.         FixMenuMarks();        /* We need to have the dis/enabling up to date */
  307.         DoMenuCommand( MenuKey( theChar ) );
  308.     }
  309.     
  310.     return;
  311. }
  312.  
  313. void FixMenuMarks( void )
  314. {
  315.     return;
  316. }
  317.  
  318. void InitManagers( void )
  319. {
  320.     MaxApplZone();
  321.     //InitGraf(&thePort);
  322.     InitGraf(&qd.thePort);
  323.     InitFonts();
  324.     /* FlushEvents(everyEvent, 0); */
  325.     InitWindows();
  326.     TEInit();
  327.     InitDialogs(0L);
  328.  
  329.     InitCursor();
  330.     
  331.     return;
  332. }
  333.  
  334. void PutUpMenus( void )
  335. {
  336.     Handle        myMenuBar;
  337.     MenuHandle    appleMenu;
  338.     MenuHandle    fontMenu;
  339.     
  340.     myMenuBar = GetNewMBar( kMBarID );
  341.     if ( !myMenuBar ){
  342.         DebugStr( "\pCannot get menu bar" );
  343.         return;
  344.     }
  345.  
  346.     appleMenu = (MenuHandle)GetResource( 'MENU', kAppleMenuID );
  347.     if ( !appleMenu ){
  348.         DebugStr( "\pcannot get Apple Menu Handle" );
  349.         return;
  350.     }
  351.     AddResMenu( appleMenu, 'DRVR' );
  352.  
  353.     SetMenuBar( myMenuBar );
  354.     DrawMenuBar();
  355.         
  356.     return;
  357. }
  358.  
  359. void DoNewWindow( void )
  360. {
  361.     PicHandle    teaParty;
  362.     Rect        r;
  363.     tTBPtr        tbPtr;
  364.     Point        tlPt;
  365.     Point        brPt;
  366.     Rect        globalR;
  367.     Rect        localR;
  368.  
  369.     if ( gDocWindow )
  370.         return;
  371.     
  372.     gDocWindow = GetNewWindow( 128, (void*)NULL, (WindowRef)-1 );
  373.     if ( !gDocWindow ){
  374.         DebugStr( "\pCan't make new window" );
  375.         return;
  376.     }
  377.     
  378.     teaParty = GetPicture( 128 );
  379.     if ( !teaParty ){
  380.         DebugStr( "\pNo tea for Michael tonight" );
  381.         return;
  382.     }
  383.  
  384.     DetachResource( (Handle)teaParty );
  385.  
  386.     r = (*teaParty)->picFrame;
  387.  
  388.     SizeWindow( gDocWindow, r.right - r.left, r.bottom - r.top, true );
  389.  
  390.     ( (WindowPeek)gDocWindow )->windowPic = teaParty;
  391.  
  392.     SetPort( gDocWindow );
  393.     
  394.     tlPt.h = 0;
  395.     tlPt.v = 0;
  396.     brPt.h = r.right - r.left;
  397.     brPt.v = r.bottom - r.top;
  398.  
  399.     SetRect( &localR, 0, 0, brPt.h, brPt.v );
  400.  
  401.     LocalToGlobal( &tlPt );
  402.     LocalToGlobal( &brPt );
  403.  
  404.     SetRect( &globalR, tlPt.h, tlPt.v, brPt.h, brPt.v );
  405.  
  406.     tbPtr = NewTBRec( gDocWindow, &globalR, &localR );
  407.     if ( !tbPtr ){
  408.         DebugStr( "\pOutta luck" );
  409.         return;
  410.     }
  411.  
  412.     ( (WindowPeek)gDocWindow )->refCon = (long)tbPtr;
  413.  
  414.     return;
  415. }
  416.  
  417. tTBPtr NewTBRec( GrafPtr port, Rect *globalBoundsPtr, Rect *localBoundsPtr )
  418. {
  419.     tTBPtr            tbPtr;
  420.     GMParamBlockPtr    paramPtr;
  421.     short            i;
  422.     MonitorsInfoPtr    monitors;
  423.     AD_QDGlobalsPtr    qdGlobalsCopy;
  424.     StringPtr        errorMessage;
  425.     SndChannelPtr    sndChannel;
  426.     RgnHandle        blankRgn;
  427.  
  428.     paramPtr = (GMParamBlockPtr)NewPtr( sizeof( GMParamBlock ) );
  429.     if ( !paramPtr )
  430.         return (tTBPtr)NULL;
  431.         
  432.     tbPtr = (tTBPtr)NewPtr( sizeof( tTBRec ) );
  433.     if ( !tbPtr )
  434.         return (tTBPtr)NULL;
  435.  
  436.     tbPtr->paramPtr = paramPtr;
  437.     tbPtr->storage = (Handle)NULL;
  438.     tbPtr->running = false;
  439.     
  440.     blankRgn = NewRgn();
  441.     if ( !blankRgn ){
  442.         return (tTBPtr)NULL;
  443.     }
  444.     RectRgn( blankRgn, localBoundsPtr );
  445.     tbPtr->blankRgn = blankRgn;
  446.  
  447.     for ( i = 0; i < 4; i++ )
  448.         paramPtr->controlValues[ i ] = 0;
  449.  
  450.     monitors = (MonitorsInfoPtr)NewPtr( sizeof( MonitorsInfo ) );
  451.     monitors->monitorCount = 1;
  452.     monitors->monitorList[ 0 ].bounds = *globalBoundsPtr;
  453.     monitors->monitorList[ 0 ].synchFlag = true;            // STUB use a slot VBL
  454.     monitors->monitorList[ 0 ].curDepth = 8;                // STUB get it from monitor    
  455.     paramPtr->monitors = monitors;
  456.     
  457.     paramPtr->colorQDAvail = true;
  458.  
  459.     /* 0xc2ff
  460.     1111
  461.     Bit 0:  cqdAvail - The Macintosh has Color QuickDraw.
  462.     Bit 1:  anyMultibit - A monitor is set to greater than 1 bit in depth.
  463.     Bit 2:  allMultibit - All monitors are set to greater than 1 bit in depth.
  464.     Bit 3:  anyColor - A monitor is in color mode.
  465.  
  466.     1111
  467.     Bit 4:  allColor - All monitors are in color mode.
  468.     Bit 5:  anyCLUTDevice - A monitor is a CLUT device.
  469.     Bit 6:  allCLUTDevice - All monitors are CLUT devices.
  470.     Bit 7:  allCanDim - All monitors can dim.
  471.  
  472.     0010
  473.     Bit 8:  mainMonCanDim - The main monitor can dim.
  474.     Bit 9:  moduleMayNotAnimate - Module may not animate the color table.
  475.     Bit 10:  multiModuleRunning - Module is running under MultiModule.
  476.     Bits 11-13:  Reserved.
  477.  
  478.     0100
  479.     Bit 14: extensionsAvailable - Module is running under After Dark 2.0u or later.
  480.     Bit 15:  soundAvail - This version of After Dark supports sound.
  481.     */
  482. #define kMikesConfig 0x42ff
  483.     paramPtr->systemConfig = kMikesConfig;
  484.     
  485.     qdGlobalsCopy = (AD_QDGlobalsPtr)NewPtr( sizeof( AD_QDGlobals ) );
  486.     if ( !qdGlobalsCopy )
  487.         return (tTBPtr)NULL;
  488.     
  489.     qdGlobalsCopy->qdThePort = qd.thePort;
  490.     qdGlobalsCopy->qdWhite = qd.white;
  491.     qdGlobalsCopy->qdBlack = qd.black;
  492.     qdGlobalsCopy->qdGray = qd.gray;
  493.     qdGlobalsCopy->qdLtGray = qd.ltGray;
  494.     qdGlobalsCopy->qdDkGray = qd.dkGray;
  495.     qdGlobalsCopy->qdArrow = qd.arrow;
  496.     qdGlobalsCopy->qdScreenBits = qd.screenBits;
  497.     qdGlobalsCopy->qdRandSeed = qd.randSeed;
  498.     paramPtr->qdGlobalsCopy = qdGlobalsCopy;
  499.     
  500.     paramPtr->brightness = 255;    
  501.     
  502.     SetRect( ¶mPtr->demoRect, 0, 0, 0, 0 );
  503.     
  504.     errorMessage = (StringPtr)NewPtr( sizeof( Str255 ) );
  505.     paramPtr->errorMessage = errorMessage;
  506.     
  507.     sndChannel = (SndChannelPtr)NULL;
  508.     paramPtr->sndChannel = sndChannel;
  509.     
  510.     paramPtr->adVersion = 0x0300;
  511.     
  512.     paramPtr->extensions = (ExtensionTablePtr)NULL;
  513.     
  514.     return tbPtr;
  515. }
  516.  
  517. void DoCloseWindow( void )
  518. {
  519.     PicHandle    teaParty;
  520.     
  521.     if ( !gDocWindow )
  522.         return;
  523.     
  524.     teaParty = ( (WindowPeek)gDocWindow )->windowPic;
  525.     
  526.     if ( teaParty ){
  527.         KillPicture( teaParty );
  528.     }
  529.     
  530.     // STUB dispose of tTBRec and its fields
  531.  
  532.     DisposeWindow( gDocWindow );
  533.     
  534.     gDocWindow = (WindowRef)NULL;
  535.     
  536.     return;
  537. }
  538.  
  539. void RequestQuit( void )
  540. {
  541.     gQuitRequested = true;
  542.  
  543.     return;
  544. }
  545.  
  546. void DoUpdate( WindowPtr win )
  547. {
  548.     GrafPtr    oldPort;
  549.     Rect    r;
  550.  
  551.     BeginUpdate( win );
  552.     
  553.     r = ( *( (WindowPeek)win )->windowPic)->picFrame;
  554.     
  555.     GetPort( &oldPort );
  556.     SetPort( win );
  557.     
  558.     DrawPicture( ( (WindowPeek)win )->windowPic, &r );
  559.     
  560.     SetPort( oldPort );
  561.     
  562.     EndUpdate( win );
  563.  
  564.     return;
  565. }